local ActorData = require("app.datacenter.ActorData")
local DyActor = {
    actors = {},
    actors_waiting = {}, --收到的actor信息，
}

function DyActor:reset()
    self.actors = {}
end

function DyActor:getActors()
    return self.actors
end

function DyActor:addActor(actordata)
    local added = false
    if self.actors[actordata.id] == nil then
        self.actors[actordata.id] = actordata 
        added = true
    else
        print("XXXXX-----add actor error-----",actordata.id)
    end
    return added
end

function DyActor:removeActor(id)
    self.actors[id] = nil
    self.actors_waiting[id] = nil
end

function DyActor:addActorWaiting(actordata)
    if self.actors_waiting[actordata.id] == nil then
        self.actors_waiting[actordata.id] = actordata 
    else
        print("XXXXX-----add actor waiting error-----",actordata.id)
    end
end

function DyActor:checkActorsWaiting()
--    for k,v in pairs(self.actors_waiting) do
--        return true
--    end  
--    return false
    return next(self.actors_waiting) ~= nil
end

function DyActor:popActorsWaiting()
    local index,actor = next(self.actors_waiting)
    self.actors_waiting[index] = nil
    return self:addActor(actor) and actor or nil
end

--islogin登录传来的协议会附加字段
function DyActor:addProtoActors(actors,islogin)
    local input = actors
    input:setPos(1)
    while input:getAvailable()> 0 do
        local actordata = ActorData.new()
        actordata._type = input:readByte()
	    actordata._bool = input:readBool()
	    actordata.id = input:readVarInt()
	    actordata.pos.x = input:readVarInt()
	    actordata.pos.y = input:readVarInt()
	    actordata.dir = input:readByte()
	    actordata.death = input:readBool()
	    actordata.serverid = input:readVarInt()
	    actordata.name = input:readString()	
	    actordata.sex = input:readByte()
	    actordata.level = input:readVarInt()
	    actordata.stageid = input:readVarInt()	
	    actordata.onlineTime = input:readVarInt()
	    actordata.mapid = input:readVarInt()
        if islogin then
            local ksign = input:readVarInt()
            while ksign > 0 do
                if bit.band(ksign,1) then
                    local to_achieve = input:readVarInt()
                end
                ksign = bit.rshift(ksign,1)
            end
        end
        self:addActorWaiting(actordata)
    end
end

return DyActor